package com.sebscape.sebcms.email;

import java.util.Properties;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sebscape.sebcms.setup.domain.SetupConfiguration;
import com.sebscape.sebcms.setup.services.SiteConfigurationServices;

public class EmailServices {
	
	private static Log log = LogFactory.getLog( EmailServices.class );
	private SetupConfiguration setupConfiguration;

	/**
	 * A constructor which uses the Request to look for the setupConfiguration: Checks the session-scope,
	 * then the request-scope.
	 */
	public EmailServices( SetupConfiguration setupConfiguration ) {
		this.setupConfiguration = setupConfiguration;
	}

	/**
	 * A constructor which uses the Request to look for the setupConfiguration: Checks the session-scope,
	 * then the request-scope.
	 */
	public EmailServices( ServletContext servletContext ) {
		this( SiteConfigurationServices.findConfiguration( servletContext ) );
	}
	
	/**
	 * 
	 * @param toAddress the address to which the email is sent.
	 * @param fromAddress set this to Null to use the SetupConfig-defined from address.
	 * @param subject Note that the SetupConfig's Prefix will be added to the beginning of this message.
	 * @param msgBody The body of the message.
	 * @return
	 */
	public Boolean sendMessage( String toAddress, String ccAddress, String bccAddress, 
			String fromAddress, String subject, String msgBody ){
		
        Properties props = new Properties();
        Session session = Session.getDefaultInstance( props, null );

        try {
            Message msg = new MimeMessage( session );
            if( fromAddress != null ){
	            log.debug("using emailFrom: " + fromAddress );
	            msg.setFrom(new InternetAddress( fromAddress ) );
            } else {
            	log.debug("using emailFrom: " + setupConfiguration.getEmailAdminFrom() );
	            msg.setFrom(new InternetAddress( setupConfiguration.getEmailAdminFrom() ) );
            }
	        log.debug("using emailTo: " + toAddress );
            msg.addRecipient( Message.RecipientType.TO, new InternetAddress( toAddress ) );
            if( ccAddress != null ){
            	log.debug("using emailCc: " + toAddress );
            	msg.addRecipient( Message.RecipientType.CC, new InternetAddress( ccAddress ) );
            }
            if( bccAddress != null ){
            	log.debug("using emailBcc: " + toAddress );
            	msg.addRecipient( Message.RecipientType.BCC, new InternetAddress( bccAddress ) );
            }
            msg.setSubject( this.setupConfiguration.getEmailAdminSubjectPrefix() + ' ' + subject );
            msg.setText(msgBody);
            Transport.send(msg);
            return Boolean.TRUE;
        } catch (AddressException e) {
            log.error( e.getMessage() );
            e.printStackTrace();
        } catch (MessagingException e) {
        	log.error( e.getMessage() );
        	e.printStackTrace();
        }
        return Boolean.FALSE;
	}
}
